package com.zcx.project.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zcx.project.entity.ZcxPlanDetail;
import com.zcx.project.entity.ZcxProblem;
import com.zcx.project.entity.ZcxTrainInfo;
import com.zcx.project.mapper.ZcxPlanDetailMapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcx.project.entity.ZcxPlanExcute;
import com.zcx.project.mapper.ZcxPlanExcuteMapper;
import com.zcx.project.mapper.ZcxProblemMapper;
import com.zcx.project.mapper.ZcxTrainInfoMapper;
import com.zcx.project.service.ZcxProblemServcie;


@Controller
@RequestMapping("/ZcxPlanExcute")
public class ZcxPlanExcuteController {

    private static final Logger log = LoggerFactory.getLogger(ZcxPlanExcuteController.class);
    @Autowired
    ZcxPlanExcuteMapper zcxPlanExcuteMapper;

    @Autowired
    ZcxPlanDetailMapper zcxPlanDetailMapper;

    @Autowired
    ZcxProblemServcie zcxProblemServcie;
    @Autowired
    ZcxTrainInfoMapper zcxTrainInfoMapper;

    @RequestMapping(value = "ZcxPlanExcuteHtml")
    public ModelAndView getMainPage(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("zcxPlanExcute");
        return mv;
    }

    @RequestMapping(value = "ZcxPlanExcuteJxHtml")
    public ModelAndView ZcxPlanExcuteJxHtml(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("zcxPlanExcute-jx");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/findList")
    public List findList(ZcxPlanExcute obj) {
        List list = zcxPlanExcuteMapper.findList(obj);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    public Map<String, Object> findZcxPlanExcutePage(int currentPage, int rows, ZcxPlanExcute obj) {

        //使用分页插件,核心代码就这一行，页数、每页行数
        PageHelper.startPage(currentPage, rows);
        List<ZcxPlanExcute> list = zcxPlanExcuteMapper.findList(obj);
        PageInfo<ZcxPlanExcute> pageInfo = new PageInfo<ZcxPlanExcute>(list);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;


    }


    @ResponseBody
    @RequestMapping(value = "/insertZcxPlanExcute")
    public Map<String, Object> addZcxPlanExcute(ZcxPlanExcute obj) {

        ZcxPlanDetail zpd = new ZcxPlanDetail();
        zpd.setId(obj.getPlanDetailId());
        zpd.setPlanDetailStatus("完成");
        zcxPlanDetailMapper.updateByPrimaryKeySelective(zpd);
        try {
            if ("是".equals(obj.getSfwx())) {
                // 增加一条问题单；
                ZcxTrainInfo zcxTrainInfo = zcxTrainInfoMapper.selectByNo(obj.getTrainNo());

                ZcxProblem zcxProblem = new ZcxProblem();
                zcxProblem.setProblemInfo(obj.getExcuteInfo());
                zcxProblem.setTrainInfoId(zcxTrainInfo.getId());
                zcxProblem.setUserId(obj.getUserId());
                zcxProblemServcie.addZcxProblem(zcxProblem);
            }
        } catch (Exception e) {
            log.info("新增问题单失败，", e);
        }

        int i = zcxPlanExcuteMapper.insertSelective(obj);

        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "添加失败！");
        else
            returnMap.put("showInfo", "添加成功！");
        return returnMap;
    }


    @ResponseBody
    @RequestMapping(value = "/updateZcxPlanExcute")
    public Map<String, Object> updateZcxPlanExcute(ZcxPlanExcute obj) {
        int i = zcxPlanExcuteMapper.updateByPrimaryKeySelective(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "修改失败！");
        else
            returnMap.put("showInfo", "修改成功！");
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteZcxPlanExcute")
    public Map<String, Object> deleteZcxPlanExcute(int id) {
        int i = zcxPlanExcuteMapper.deleteByPrimaryKey(id);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "删除失败！");
        else
            returnMap.put("showInfo", "删除成功！");
        return returnMap;
    }


}
